//
//  SingerThirdViewController.m
//  MusicPlayer
//
//  Created by apple on 15/5/23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
#import "ZhuanViewController.h"
#import "PlayMusicViewController.h"
#import "SingerThirdViewController.h"
#import "SANetWorkingTask.h"
#import <UIImageView+WebCache.h>
#import "SingerZhuanTableViewCell.h"
#import "SingerSongTableViewCell.h"
#import "SingerVideoTableViewCell.h"
#import "RecommendLatestModel.h"
@interface SingerThirdViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain) NSMutableArray * array;
@property (nonatomic,retain) UITableView * tableView;
@property (nonatomic, retain) UITableView * songTable;
@property (nonatomic, retain) UITableView * videoTable;
@property (nonatomic, retain) UIScrollView *scroll;
@property (nonatomic, retain) NSMutableDictionary * dic;
@property (nonatomic, retain) NSMutableArray * zhuanjiArray;
@property (nonatomic, retain) NSMutableArray * songArray;
@property (nonatomic, retain) NSMutableArray * videoArray;
@property (nonatomic, retain)  UIImageView * image;

@property (nonatomic, retain) NSMutableArray *urlArray;
@property (nonatomic, retain) NSMutableArray *songListArray;
@property (nonatomic, retain) UIImageView *myImageView;
@property (nonatomic, retain) UIButton *abutton;
@property (nonatomic, retain) UIButton * bbutton;
@end

@implementation SingerThirdViewController

- (void)dealloc
{
    [_urlArray release];
    [_array release];
    [_website release];
    [_zhuanji release];
    [_zhuanjiArray release];
    [_song release];
    [_video release];
    [_songListArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.dic = [NSMutableDictionary dictionary];
        self.zhuanjiArray = [NSMutableArray array];
        self.urlArray = [NSMutableArray array];
        self.songListArray = [NSMutableArray array];
    }
    return self;
}




-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.scroll];
    [self.view bringSubviewToFront:self.image];
    [self.view bringSubviewToFront:self.abutton];
    [self.view bringSubviewToFront:self.bbutton];
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationItem.title = self.aTitle;
    [self createScrollView];
    [self TableView];
    
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    
    //    UIImageVIew数据请求
    self.website = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@?detail=true&bundle_id=com.ttpod.music",self.SingerDetailId];
    [SANetWorkingTask request:self.website parmater:nil block:^(NSDictionary *result) {
        self.dic = [result objectForKey:@"data"];
        
        // 创建imageView
        self.image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height/667.0* 270)];
        [self.image sd_setImageWithURL:[NSURL URLWithString:[self.dic objectForKey:@"picUrl"]]];
        self.image.userInteractionEnabled = NO;
        [self.view addSubview:self.image];
        
        [self createView];
    }];
    
    //    歌单数据请求
    self.song = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/songs?page=1&size=50&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=a32e3f40acd47460a764124ccb6894dcb5e20200&idfa=E2FD8DB1-FBA8-40EA-8C02-FAB446839AD1&utdid=VV",self.SingerDetailId];
    [SANetWorkingTask request:self.song parmater:nil block:^(NSDictionary *result) {
        self.songArray = [result objectForKey:@"data"];
        
        //        获取所有URL
        for (NSDictionary *dic in self.songArray) {
            
            RecommendLatestModel *model = [[RecommendLatestModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            model.song_name = model.name;
            model.singer_name = model.singerName;
            [self.songListArray addObject:model];
            
            NSArray *arr = [dic objectForKey:@"urlList"];
            if (arr.count >= 2) {
                NSDictionary *dic1 = [arr objectAtIndex:1];
                NSString *url = [dic1 objectForKey:@"url"];
                [self.urlArray addObject:url];
            }
        }
        
        [self.songTable reloadData];
    }];
    //  专辑数据请求
    self.zhuanji = [NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/albums?page=1&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=a32e3f40acd47460a764124ccb6894dcb5e20200&idfa=E2FD8DB1-FBA8-40EA-8C02-FAB446839AD1&utdid=VV",self.SingerDetailId];
    [SANetWorkingTask request:self.zhuanji parmater:nil block:^(NSDictionary *result) {
        self.zhuanjiArray = [result objectForKey:@"data"];
        [self.tableView reloadData];
    }];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    
}


#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//创建标题
- (void)createView
{
    // 创建2个button，分别赋值
    self.abutton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 * 0 , self.view.frame.size.height/667.0* 280, self.view.frame.size.width / 2 + self.view.frame.size.width/375.0* 10, self.view.frame.size.height/667.0* 50)];
    [self.abutton setTitle:@"专辑" forState:UIControlStateNormal];
    self.abutton.userInteractionEnabled = YES;
    self.abutton.titleLabel.font = [UIFont systemFontOfSize:self.view.frame.size.width/375.0* 17];
    self.abutton.tag = 1;
    [self.abutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.abutton addTarget:self action:@selector(buttonaa:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.abutton];
    
    self.bbutton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 * 1 , self.view.frame.size.height/667.0* 280, self.view.frame.size.width / 2 + self.view.frame.size.width/375.0* 10, self.view.frame.size.height/667.0* 50)];
    [self.bbutton setTitle:@"单曲" forState:UIControlStateNormal];
    self.bbutton.userInteractionEnabled = YES;
    self.abutton.titleLabel.font = [UIFont systemFontOfSize:self.view.frame.size.width/375.0* 17];
    self.bbutton.tag = 2;
    [self.bbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.bbutton addTarget:self action:@selector(buttonaa:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.bbutton];
}

// button的点击事件
- (void)buttonaa:(id)sender
{
    // 从上面获取button的tag值，计算scrollView的偏移量
    UIButton * button = (UIButton *)sender;
    self.scroll.contentOffset = CGPointMake(self.view.frame.size.width * (button.tag - 1), 0);
}

//创建滚动条
- (void)createScrollView
{
    self.scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height/667.0* 300, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/667.0* 290)];
    self.scroll.pagingEnabled = YES;
    self.scroll.contentSize = CGSizeMake(self.view.frame.size.width * 2,0);
    [self.view addSubview:self.scroll];
    [self.scroll release];
    
}

//创建单曲，专辑tableview
- (void)TableView
{
    //    专辑tableview
    
    UIView * table = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/667.0* 70 - self.view.frame.size.height/667.0* 64)];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/667.0 * 64 -self.view.frame.size.height/667.0 * 360) style:UITableViewStylePlain];
    
    _tableView.backgroundColor = [UIColor clearColor];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.tableView.rowHeight =self.view.frame.size.width/375.0* 100;
    [table addSubview:self.tableView];
    [self.scroll addSubview:table];
    [table release];
    [self.tableView release];
    
    //    单曲tableView
    
    UIView * song = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height- self.view.frame.size.height/667.0* 70 - self.view.frame.size.height/667.0* 64)];
    self.songTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height/667.0* 380 - self.view.frame.size.height/667.0* 64) style:UITableViewStylePlain];
    
    song.backgroundColor = [UIColor clearColor];
    self.songTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 360) style:UITableViewStylePlain];
    self.songTable.backgroundColor = [UIColor clearColor];
    
    self.songTable.dataSource = self;
    self.songTable.delegate = self;
    self.songTable.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.songTable.rowHeight =self.view.frame.size.width/375.0*  80;
    [song addSubview:self.songTable];
    [self.scroll addSubview:song];
    [self.songTable release];
    [song release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.songTable) {
        return _songArray.count;
    } else if (tableView == self.tableView) {
        return _zhuanjiArray.count;
    }  else {
        
        return _videoArray.count;
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //    滚动时候判断时哪个tableView
    
    //    歌单
    if (tableView == self.songTable) {
        static NSString * cellIdentify = @"cell";
        SingerSongTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!cell) {
            cell = [[[SingerSongTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
        }
        cell.backgroundColor = [UIColor clearColor];
        NSMutableDictionary * songDic = [self.songArray objectAtIndex:indexPath.row];
        cell.name.text = [songDic objectForKey:@"name"];
        cell.name.textColor = [UIColor whiteColor];
        cell.singerName.text = [songDic objectForKey:@"singerName"];
        cell.singerName.textColor = [UIColor whiteColor];
        return cell;
        
    } else {
        //        专辑
        static NSString * cellIdentify = @"cell";
        SingerZhuanTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!cell) {
            cell = [[[SingerZhuanTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify] autorelease];
        }
        cell.backgroundColor = [UIColor clearColor];
        NSMutableDictionary * zhuanDic = [self.zhuanjiArray objectAtIndex:indexPath.row];
        cell.zhuanName.text = [zhuanDic objectForKey:@"name"];
        cell.zhuanName.textColor = [UIColor whiteColor];
        cell.time.text = [zhuanDic objectForKey:@"publishDate"];
        cell.time.textColor = [UIColor whiteColor];
        NSString * url = [zhuanDic objectForKey:@"picUrl"];
        [cell.image sd_setImageWithURL:[NSURL URLWithString:url]];
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == self.songTable) {
        //        进行单曲播放
        RecommendLatestModel *model = [self.songListArray objectAtIndex:indexPath.row];
        NSString *str = [self.urlArray objectAtIndex:indexPath.row];
        PlayMusicViewController *playMusic = [[PlayMusicViewController alloc] init];
        model.song_name = model.name;
        model.singer_name = model.singerName;
        playMusic.songName = model.name;
        playMusic.singer = model.singerName;
        playMusic.songUrl = str;
        playMusic.songArray = self.urlArray;
        playMusic.songListArray = self.songListArray;
        playMusic.num = indexPath.row;
        
        [self.navigationController pushViewController:playMusic animated:YES];
        
    } else if (tableView == self.tableView) {
        
        //        专辑详情歌单页面
        ZhuanViewController * zhuan = [[ZhuanViewController alloc]init];
        NSMutableDictionary * dic = [self.zhuanjiArray objectAtIndex:indexPath.row];
        //        传值
        zhuan.detailID = [dic objectForKey:@"coverId"];
        [self.navigationController pushViewController:zhuan animated:YES];
    }
}
/*
 #pragma mark - Navigationß
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
